home *** CD-ROM | disk | FTP | other *** search
/ Aminet 35 / Aminet 35 (2000)(Schatztruhe)[!][Feb 2000].iso / Aminet / dev / src / td01_src.lha / td_r0.1 / sl3 / source / openglcs / opengl.c next >
Encoding:
C/C++ Source or Header  |  1999-04-11  |  5.1 KB  |  161 lines

  1. /*
  2. **      $VER: opengl.c 1.00 (10.4.1999)
  3. **
  4. **      Creation date : 10.4.1999
  5. **
  6. **      Description       :
  7. **         Standart saver module for meshwriter.library.
  8. **         Saves the mesh as OpenGL C-source code.
  9. **
  10. **
  11. **      Written by Stephan Bielmann
  12. **
  13. */
  14.  
  15. /*************************** Includes *******************************/
  16.  
  17. /*
  18. ** Amiga includes
  19. */
  20. #include <dos/dos.h>
  21. #include <dos/stdio.h>
  22.  
  23. #include <clib/dos_protos.h>
  24. #include <clib/alib_stdio_protos.h>
  25.  
  26. /*
  27. ** Project includes
  28. */
  29. #include "meshwriter_private.h"
  30.  
  31. /********************** Private functions ***************************/
  32.  
  33. /********************** Public functions ****************************/
  34.  
  35. /********************************************************************\
  36. *                                                                    *
  37. * Name         : write3OPENGL                                        *
  38. *                                                                    *
  39. * Description  : Writes a standart OpenGL C-source code.             *
  40. *                                                                    *
  41. * Arguments    : vrmlfile IN : An already opened file stream.        *
  42. *                mesh     IN : Pointer to the mesh.                  *
  43. *                                                                    *
  44. * Return Value : RCNOERROR                                           *
  45. *                RCWRITEDATA                                         *
  46. *                                                                    *
  47. * Comment      : Default material is the first.                      *
  48. *                                                                    *
  49. \********************************************************************/
  50. ULONG write3OPENGL(BPTR openglfile, TOCLMesh *mesh) {
  51.     UBYTE                         buffer[200];
  52.     TOCLVertexNode                *ver=NULL;
  53.     TOCLMaterialNode            *mat=NULL;
  54.     TOCLPolygonNode             *pln=NULL;
  55.     TOCLPolygonsVerticesNode    *plv=NULL;
  56.     TOCLFloat                    r,g,b;
  57.             
  58.     /*
  59.     ** Write the header, the copyright, default separator and info node
  60.     */
  61.     if (FPuts(openglfile,"/*\n**\n** MeshWriter OpenGL C-source output\n**\n")!=DOSFALSE) return(RCWRITEDATA);
  62.     if (mesh->name) {
  63.         if (FPrintf(openglfile,"** Mesh      : %s\n**\n",mesh->name)==ENDSTREAMCH) return(RCWRITEDATA);      
  64.     }
  65.     if (mesh->copyright) {
  66.         if (FPrintf(openglfile,"** Copyright : %s\n**\n",mesh->copyright)==ENDSTREAMCH) return(RCWRITEDATA);      
  67.     }
  68.     if (FPrintf(openglfile,"**\n*/\n\n")==ENDSTREAMCH) return(RCWRITEDATA);
  69.  
  70.     /*
  71.     ** Function header
  72.     */
  73.     if (FPuts(openglfile,"static void mesh () {\n")!=DOSFALSE) return(RCWRITEDATA);    
  74.  
  75.     /*
  76.     ** Write the light and camera position
  77.     */
  78.     sprintf(buffer,"  static GLfloat lp[4] = {%g,%g,%g,0.0};\n",mesh->light.position.x,mesh->light.position.z,mesh->light.position.y);
  79.     if (FPuts(openglfile,buffer)!=DOSFALSE) return(RCWRITEDATA);
  80.  
  81.     /*
  82.     ** Write the material array
  83.     */
  84.     if(mesh->materials.firstNode!=NULL) {
  85.           
  86.         mat=mesh->materials.firstNode;
  87.         if (FPuts(openglfile,"  static GLfloat m[] = {\n")!=DOSFALSE) return(RCWRITEDATA);
  88.         do {
  89.             TOCLColor col=mat->diffuseColor;
  90.             r=col.r,g=col.g,b=col.b;
  91.             sprintf(buffer,"    %g,%g,%g,1.0,\n",r/255,g/255,b/255);
  92.             if (FPuts(openglfile,buffer)!=DOSFALSE) return(RCWRITEDATA);
  93.             mat=mat->next;
  94.         } while(mat!=NULL);
  95.         if (FPuts(openglfile,"  };\n\n")!=DOSFALSE) return(RCWRITEDATA);                
  96.     }
  97.  
  98.     /*
  99.     ** Write the vertice array
  100.     */              
  101.     if(mesh->vertices.firstNode!=NULL) {
  102.         ver=mesh->vertices.firstNode;
  103.         if (FPuts(openglfile,"  static GLfloat v[] = {\n")!=DOSFALSE) return(RCWRITEDATA);
  104.         do {
  105.             TOCLVertex v=ver->vertex;
  106.             sprintf(buffer,"    %g,%g,%g,\n",v.x,v.y,v.z);
  107.             if(FPuts(openglfile,buffer)!=DOSFALSE) return(RCWRITEDATA);
  108.             ver=ver->next;
  109.         } while(ver!=NULL);
  110.         if (FPuts(openglfile,"  };\n\n")!=DOSFALSE) return(RCWRITEDATA);
  111.     }
  112.  
  113. //!!!!!!!!!!!
  114.     /*
  115.     ** Write the light source
  116.     */
  117.     r=mesh->light.color.r;
  118.     g=mesh->light.color.g;
  119.     b=mesh->light.color.b;
  120.     r/=255,g/=255,b/=255;
  121.     sprintf(buffer,"  glLightfv(GL_LIGHT0,GL_POSITION,lp);\n",
  122.             r,g,b,mesh->light.position.x,mesh->light.position.y,mesh->light.position.z);
  123.     if(FPuts(openglfile,buffer)!=DOSFALSE) return(RCWRITEDATA);
  124.  
  125.  
  126.  
  127.     /*
  128.     ** Write the polygons with theyr material binding
  129.     */
  130.       if(mesh->polygons.firstNode!=NULL) {             
  131.             pln=mesh->polygons.firstNode;
  132.         do {                
  133.             if(pln->firstNode!=NULL) {  
  134.  
  135.                 if (pln->materialNode!=NULL) {
  136.                     if (FPrintf(openglfile,"  glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,&m[%ld]);\n",4*(pln->materialNode->index-1))==ENDSTREAMCH) return(RCWRITEDATA);
  137.                 }
  138.  
  139.                 if (FPuts(openglfile,"  glBegin(GL_POLYGON);\n")!=DOSFALSE) return(RCWRITEDATA);
  140.                 plv=pln->firstNode;
  141.                 do {
  142.                     if (FPrintf(openglfile,"    glVertex3fv(&v[%ld]);\n",3*(plv->vertexNode->index-1))==ENDSTREAMCH) return(RCWRITEDATA);
  143.  
  144.                     plv=plv->next;
  145.                 } while(plv!=NULL);
  146.                 if (FPuts(openglfile,"  glEnd();\n\n")!=DOSFALSE) return(RCWRITEDATA);
  147.             }
  148.             pln=pln->next;
  149.         } while(pln!=NULL);
  150.     }
  151.     
  152.     /*
  153.     ** Write the shae model and end of function and file
  154.     */
  155.     if (FPuts(openglfile,"  glShadeModel(GL_SMOOTH);\n}\n")!=DOSFALSE) return(RCWRITEDATA);
  156.  
  157.     return(RCNOERROR);
  158. }
  159.  
  160. /************************* End of file ******************************/
  161.